#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without filename for old histograms!"
	exit 0
fi

if [ "$2" = "" ]; then
	echo "$0 called without filename for new histograms!"
	exit 0
fi

# Replace the histogram in the EXP file - using EXPEDT resets parameters we do not want to reset
# (absorption, profile etc.)
# add a few extra spaces in the new file as the following cnvfile messes up if the new lines are too short
sed -i "s/$1/$2                  /g" `cat GSAS_EXP`.EXP
# make all lines length 80 again
awk '{printf("%-80.80s\r\n", $0) }' `cat GSAS_EXP`.EXP > TEMP.EXP
mv TEMP.EXP `cat GSAS_EXP`.EXP

# run cnvfile to re-format
echo `cat GSAS_EXP`.EXP > temp.txt
echo "" >> temp.txt
cnvfile < temp.txt

if [ "$OS" = "Windows_NT" ]; then
	echo "I think I run on a Windows box..."
	unix2dos `cat GSAS_EXP`.EXP
else
	echo "I think I run on a Unix box..."
fi

# Test the EXP file
echo "Replaced all references to \verb=$1= with \verb=$2= .\newline" >> `cat GSAS_EXP`.tex

